---
title: "Lab 2 Code"
author: "Fabio Setti"
date: "Spring-2026"
output: html_document
---

## Today's Packages and Data 🤗



```{r}
library(tidyverse)

theme_set(
  theme_classic(base_size = 16, 
                accent = "#3492eb",
                base_family = 'serif'))

dat <- rio::import("https://fabio-setti.netlify.app/data/NY_Temp.txt")
```

## Recapping the Regression model


```{r}
reg <- lm(Temp ~ Wind, dat)
summary(reg)
```

## Linear Transformations


```{r}
dat$Wind_new <- dat$Wind*3 + 4

ggplot(dat, 
       aes(x = Wind, y = Wind_new)) +
        geom_point()
```

## Mean-Centering and Standardization in `R`

```{r}
dat$Wind_cnt <- scale(dat$Wind, scale = FALSE)
dat$Temp_cnt <- scale(dat$Temp, scale = FALSE)
```



```{r}
dat$Wind_std <- scale(dat$Wind)
dat$Temp_std <- scale(dat$Temp)
```


```{r}
colnames(dat)
```

## Mean-centered regression

```{r}
reg_cnt <- lm(Temp_cnt ~ Wind_cnt, dat)
summary(reg_cnt)
```

## Standardized regression

```{r}
reg_std <- lm(Temp_std ~ Wind_std, dat)
summary(reg_std)
```


```{r}
cor(dat$Temp, dat$Wind)
```

## Looking at It graphically

```{r}
ggplot(dat, aes(x = Wind, Temp)) +
  geom_point(shape  = 4) +
  geom_smooth(method = "lm", se = FALSE, col = "blue")
```


```{r}
ggplot(dat, aes(x = Wind_cnt, Temp_cnt)) +
  geom_point(shape = 2) +
  geom_smooth(method = "lm", se = FALSE)
```


```{r}
ggplot(dat, aes(x = Wind_std, Temp_std)) +
  geom_point(shape = 3) +
  geom_smooth(method = "lm", se = FALSE, col = "red")
```

## On the same plot?


```{r}
# Combine your data into a long format or add a "type" variable
dat_long <- data.frame(
  x = c(dat$Wind_std, dat$Wind, dat$Wind_cnt),
  y = c(dat$Temp_std, dat$Temp, dat$Temp_cnt),
  type = factor(rep(c("Standardized", "Original", "Mean-Centered"), 
                    times = c(nrow(dat), nrow(dat), nrow(dat))))
)

ggplot() +
  # Points with legend
  geom_point(data = dat, aes(x = Wind_std, y = Temp_std, shape = "Standardized")) +
  geom_point(data = dat, aes(x = Wind, y = Temp, shape = "Original")) +
  geom_point(data = dat, aes(x = Wind_cnt, y = Temp_cnt, shape = "Mean-Centered")) +
  
  # Regression lines without legend
  geom_smooth(data = dat, aes(x = Wind_std, y = Temp_std), method = "lm", se = FALSE, color = "red", show.legend = FALSE) +
  geom_smooth(data = dat, aes(x = Wind, y = Temp), method = "lm", se = FALSE, color = "blue", show.legend = FALSE) +
  geom_smooth(data = dat, aes(x = Wind_cnt, y = Temp_cnt), method = "lm", se = FALSE, show.legend = FALSE) +
  
  # Labels and manual shapes
  labs(
    x = "Wind",
    y = "Temp",
    shape = ""
  ) +

scale_shape_manual(values = c("Standardized" = 3, "Original" = 4, "Mean-Centered" = 2))
```

## Hypothesis Tests


```{r}
summary(reg)$coef
```

## What about the Other Slopes?

```{r}
summary(reg)$coef 
```


```{r}
summary(reg_cnt)$coef
```


```{r}
summary(reg_std)$coef
```

## Confidence Intervals


```{r}
confint(reg)
```


```{r}
confint(reg_cnt)
```


```{r}
confint(reg_std)
```